package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.OrderNumberGenerator;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.domain.DisassembleTask;
import com.ruoyi.wms.domain.Process;
import com.ruoyi.wms.dto.DisassembleTaskDto;
import com.ruoyi.wms.dto.ProcessDto;
import com.ruoyi.wms.service.IDisassembleTaskService;
import com.ruoyi.wms.service.IProcessService;
import com.ruoyi.wms.vo.DisassemblePipelineVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.DisassemblePipelineMapper;
import com.ruoyi.wms.domain.DisassemblePipeline;
import com.ruoyi.wms.service.IDisassemblePipelineService;

/**
 * 拆解流水线Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-16
 */
@Service
public class DisassemblePipelineServiceImpl implements IDisassemblePipelineService {
    @Autowired
    private DisassemblePipelineMapper disassemblePipelineMapper;
    @Autowired
    private IProcessService processService;

    @Autowired
    private IDisassembleTaskService disassembleTaskService;


    /**
     * 查询拆解流水线
     *
     * @param id 拆解流水线主键
     * @return 拆解流水线
     */
    @Override
    public DisassemblePipelineVo selectDisassemblePipelineById(String id) {
        DisassemblePipelineVo disassemblePipelineVo = disassemblePipelineMapper.selectDisassemblePipelineById(id);

        DisassembleTaskDto disassembleTaskDto =new DisassembleTaskDto();
        disassembleTaskDto.setPlateNo( disassemblePipelineVo.getPlateNo());
        List<DisassembleTask> disassembleTasks = disassembleTaskService.selectDisassembleTaskList(disassembleTaskDto);
        disassemblePipelineVo.setTaskList( disassembleTasks);
        return disassemblePipelineVo;
    }

    /**
     * 查询拆解流水线列表
     *
     * @param disassemblePipeline 拆解流水线
     * @return 拆解流水线
     */
    @Override
    public List<DisassemblePipeline> selectDisassemblePipelineList(DisassemblePipeline disassemblePipeline) {
        return disassemblePipelineMapper.selectDisassemblePipelineList(disassemblePipeline);
    }

    /**
     * 新增拆解流水线
     *
     * @param disassemblePipeline 拆解流水线
     * @return 结果
     */
    @Override
    public int insertDisassemblePipeline(DisassemblePipeline disassemblePipeline) {
        disassemblePipeline.setId(MD5Utils.md5(disassemblePipeline.getPlateNo()));
        disassemblePipeline.setCreateUser(SecurityUtils.getUsername());
        disassemblePipeline.setCreateDate(DateUtils.getCurrentTime());
        disassemblePipeline.setUpdateDate( DateUtils.getCurrentTime());

        return disassemblePipelineMapper.insertDisassemblePipeline(disassemblePipeline);
    }

    /**
     * 修改拆解流水线
     *
     * @param disassemblePipeline 拆解流水线
     * @return 结果
     */
    @Override
    public int updateDisassemblePipeline(DisassemblePipeline disassemblePipeline) {

        int updateResult = 0;
        if (StringUtils.isEmpty(disassemblePipeline.getId())) {
            updateResult = insertDisassemblePipeline(disassemblePipeline);
        }
        disassemblePipeline.setUpdateDate( DateUtils.getCurrentTime());
        updateResult =disassemblePipelineMapper.updateDisassemblePipeline(disassemblePipeline);
        if (0 == updateResult) {
            updateResult = insertDisassemblePipeline(disassemblePipeline);
        }


        return updateResult;


    }

    /**
     * 批量删除拆解流水线
     *
     * @param ids 需要删除的拆解流水线主键
     * @return 结果
     */
    @Override
    public int deleteDisassemblePipelineByIds(String[] ids) {
        return disassemblePipelineMapper.deleteDisassemblePipelineByIds(ids);
    }

    /**
     * 删除拆解流水线信息
     *
     * @param id 拆解流水线主键
     * @return 结果
     */
    @Override
    public int deleteDisassemblePipelineById(String id) {
        return disassemblePipelineMapper.deleteDisassemblePipelineById(id);
    }

    @Override
    public int disassemble(String[] ids) {
    int flag =0;
        List<Process> processesList  = processService.selectProcessList(new ProcessDto());

//        修改拆解时间
        for(String id  :ids ){
            DisassemblePipeline disassemblePipeline = disassemblePipelineMapper.selectDisassemblePipelineById(id);
            if(null == disassemblePipeline){
               break;
            }



            disassemblePipeline.setDisassemblyTime( DateUtils.getCurrentTime());
            disassemblePipelineMapper.updateDisassemblePipeline(disassemblePipeline);


            for (Process pro:processesList ){
                DisassembleTask task =new DisassembleTask();
                BeanUtils.copyProperties(pro , task );
                task.setPlateNo(disassemblePipeline.getPlateNo() );
                task.setTaskNum(OrderNumberGenerator.generateOrderNumber());
                task.setDismantler(pro.getdismantler());
                task.setPipelineId(disassemblePipeline.getId() );
                flag= disassembleTaskService.insertDisassembleTask(task);
            }
        }




        return flag;
    }
}
